﻿using System;
using System.IO;
using System.Linq;

namespace MainScript
{
    public class Person
    {
        private SinteticPerson _base;

        public string FullName { get { return _base.FullName; } set { _base.FullName = value; } }
        public int Mark { get { return _base.Mark; } set { _base.Mark = value; } }
        public string Message { get { return _base.Message; } set { _base.Message = value; } }

        public string CorrectName { get { return FullName.Replace('_', ' '); } private set { } }

        public Tuple<bool, string, string> NameMatch { get; private set; }

        public string Name
        {
            get
            {
                return FullName.Split(" ".ToCharArray())[0];
            }
            private set { }
        }

        public string Surname
        {
            get
            {
                return FullName.Split(" ".ToCharArray())[1];
            }
            private set { }
        }

        public Person(string path)
        {
            _base = new SinteticPerson(path);
            NameMatch = new Tuple<bool, string, string>(false, CorrectName, "");
        }

        public SinteticPerson ToSinteticPerson()
        {
            return _base;
        }

        public bool IsSame(string d)
        {
            var passed_parts = d.ToLower().Split("_ ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries).Distinct().ToList();
            var name_parts = FullName.ToLower().Split("_ ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries).Distinct().ToList();

            if (passed_parts.Count != name_parts.Count())
                return false;

            int match = 0;
            foreach (var part in passed_parts)
            {
                if (name_parts.Contains(part))
                    match++;
            }

            NameMatch = new Tuple<bool, string, string>(match == name_parts.Count, CorrectName, d);

            return match == name_parts.Count; //d.Split(new char[] { Path.DirectorySeparatorChar }).Last() == FullName;
        }

        public override string ToString()
        {
            return string.Format("{0}|{1}|{2}|{3}|{4}|{5}", CorrectName, Mark, Message, NameMatch.Item1, NameMatch.Item2, NameMatch.Item3).Replace(Environment.NewLine, "; "); //base.ToString();
        }

        public static Person Unzippable(string path)
        {
            Person ret = new Person(path);

            ret.Mark = 0;
            ret.Message = "Il file sottomesso deve essere un archivio zip valido.";

            return ret;
        }
    }

    public class SinteticPerson
    {
        public string FullName { get; set; }
        public int Mark { get; set; }
        public string Message { get; set; }

        public SinteticPerson(string path)
        {
            FullName = new DirectoryInfo(path).Name; //path.Split(new char[] { Path.DirectorySeparatorChar }).Last();
            Mark = 0;
            Message = "";
        }
    }


}
